package com.zking.dao;

import com.zking.entity.Food;
import com.zking.entity.User;
import org.apache.commons.dbutils.handlers.ArrayHandler;

import java.sql.SQLException;
import java.util.List;

public class FoodDao extends BaseDaoImpl {
    /**
     * 查询所有订单
     *
     * @return
     */
    public List<Food> queryfood() {
        String sql = "select * from food";
        try {
            return findList(Food.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
        查询商店和分类
    */
    public List<Food> queryFoodName(String sort, String shop) {
        String sql = "select * from food where sort= '" + sort + "' and shop = '" + shop + "'; ";
        try {
            return findList(Food.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Food> queryFoodSort(String sort) {
        String sql = "select * from food where sort= '" + sort + "'; ";
        try {
            return findList(Food.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
        添加菜品
     */
    public int InsertFood(String name , double price , String shop , String sort ,String ptadd, String stadd, String show){
      //  String sql ="insert into user values(?,?,?,?,?,?)";
        String sql ="INSERT INTO `rz1217db_2`.`food`(`name`, `price`, `shop`,`sort`, `Pictureaddress`, `Storeaddress`, `show`) VALUES (?,?,?,?,?,?,?)";
        Object[] params = {name,price,shop,sort,ptadd,stadd,show};
        try {
            System.out.println(runner.update(sql,params));
          //  System.out.println(update(sql,params));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public int DeleteFood(String name){
        String sql ="DELETE FROM `rz1217db_2`.`food` WHERE `name` = ?";
        //  DELETE FROM `rz1217db_2`.`food` WHERE `name` = '123456' AND `price` = 133.00 AND `shop` = '大米先生' AND `sort` = '简餐便当' AND `Pictureaddress` = '123' AND `Storeaddress` = '123' AND `show` = '123' LIMIT 1
        Object[] params = {name};
        try {
            System.out.println(runner.update(sql,params));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public int UpdateFood(String name , double price , String shop , String sort ,String ptadd, String stadd, String show,String txt_name){
        String sql = "UPDATE `rz1217db_2`.`food` SET `name` = ?, `price` = ?, `shop` = ?, `sort` = ?, `Pictureaddress` = ?, `Storeaddress` = ?, `show` = ? WHERE `name` = ? ";
        // UPDATE `rz1217db_2`.`food` SET `name` = '1111', `price` = 10.00, `shop` = 'a', `sort` = 'as', `Pictureaddress` = 'dd', `Storeaddress` = 'dbad', `show` = 'ddd' WHERE `name` = 'asda' AND `price` = 110.00 AND `shop` = 'adad' AND `sort` = 'asda' AND `Pictureaddress` = 'asda' AND `Storeaddress` = 'asda' AND `show` = 'dad' LIMIT 1
        Object[] params = {name,price,shop,sort,ptadd,stadd,show,txt_name};
        try {
            System.out.println(runner.update(sql,params));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    /*
    查询注册管理员里面是否存在商铺名称
     */
    public List<Food> queryShop(String shop){
        String sql = " select * from food  where shop = '" + shop + "'; ";
        try {
            return findList(Food.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
        管理员注册
     */
    public int InsertAdministrators(User user){
        //  String sql ="insert into user values(?,?,?,?,?,?)";
        String sql ="INSERT INTO `rz1217db_2`.`user`( `userName`, `passWord`,  `phoneNumber`,`shopName`,`sort`) VALUES (?,?,?,?,?)";
        Object[] params = {user.getUserName(),user.getPassWord(),user.getPhoneNumber(),user.getShopname(),user.getSort()};
        try {
            runner.update(sql,params);
            //  System.out.println(update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String querySort(String sort){
        String sql = "select sort from food where shop = ?";
        try {
            Object[] query = runner.query(sql, new ArrayHandler(), sort);
            return String.valueOf(query[0]);
         // System.out.println(String.valueOf(query[0]));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}




